<template>
  <el-form ref="form" label-width="200px">
    <h2 style="text-align:center;">创建新浪屿</h2>

    <el-form-item label="浪屿名称" style="width: 1000px">
      <el-input v-model="island.name"></el-input>
    </el-form-item>

    <el-form-item label="浪屿背景">
      <el-upload
          class="avatar-uploader"
          action=""
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeBackgroundUpload">
        <img v-if="islandBackground" :src="islandBackground" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>

    <el-form-item label="浪屿简介">
      <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="island.description" style="width: 800px;">
      </el-input>
    </el-form-item>
    <el-form-item label="浪屿类型" style="width: 1300px;">
      <el-select v-model="island.type" placeholder="请选择">
        <el-option v-for="item in type" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="搜索团队">
      <el-select v-model="bindTeamId"  filterable remote
                 reserve-keyword placeholder="请输入团队昵称关键词" :remote-method="queryTeam" :loading="loading"
                 style="min-width: 450px;">
        <el-option
            v-for="item in queryTeamList"
            :key="item.name"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="浪屿描述">
      <div  style="width: 1120px;margin-top: 20px">
        <WangEditor :intro-html="island.introduction" @setDescription="setDescription"/>
      </div>
    </el-form-item>

    <div
        style="width: 100%;display: flex;flex-direction: row;align-items: center;justify-content: center;margin-top: 20px;margin-bottom: 20px">
      <el-button type="primary" @click="addIsland">保存并下一步</el-button>
    </div>
  </el-form>
</template>

<script>
import {client} from "@/utils/oss.js";
import WangEditor from "@/components/WangEditor.vue";
export default {
  components:{WangEditor},
  data() {
    return {
      island: {},
      type: [
        {
          value: '0',
          label: '共创记忆'
        }, {
          value: '1',
          label: '共创文章'
        }, {
          value: '2',
          label: '共创帖子流'
        },
      ],
      statusValue: '',
      typeValue: '',
      active: 0,
      islandBackground:null,
      disabled:false,
      queryTeamList :[],
      bindTeamId: "",
      inputIntro:""
    }
  },
  methods: {
    setIntroduction: function(html) {
      this.island.introduction = html;//在父组件修改值
    },
    addIsland() {
      let that = this
      this.$confirm('是否确认添加浪屿信息', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.island.background = this.islandBackground
        this.island.status = 0
        this.island.teamId = this.bindTeamId
        this.island.introduction = this.inputIntro
        this.$api.addIsland(this.island)
            .then(response => {
              this.$message({
                type: 'success',
                message: '添加成功!'
              })
              that.$router.push({
                    name: 'island-edit',
                    params:
                        {
                          islandId:response.data
                        }
                  })
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消添加'
        });
      });
    },

    queryTeam(nameKey){
      this.$api.queryTeam({
        name : nameKey,
        pageSize : 100,
        pageNow : 0
      }).then(res => {
        this.queryTeamList = res.data.rows
      })
    },


    handleAvatarSuccess(res, file) {
      console.log(res,file)
    },

    beforeBackgroundUpload(file){
      client()
          .multipartUpload(file.name || Date.now().toString(), file)
          .then((result) => {
            let url = result.res.requestUrls[0];
            this.islandBackground = url.split("?upload")[0]
          });

    },

    setDescription: function (html) {
      this.inputIntro = html;//在父组件修改值
    },
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
